package com.example.bottom.sheet;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.example.bottom.R;
import com.google.android.material.bottomsheet.BottomSheetBehavior;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

/***
 * https://www.jianshu.com/p/e9e9931c510c
 */
public class BottomSheetActivity extends AppCompatActivity {

    private static final String TAG = "zxd";

    //有BottomSheetBehavior行为的根布局
    private LinearLayout mLlBottomSheet;
    private BottomSheetBehavior mBottomSheetBehavior;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bottom_sheet);
        // get the bottom sheet view
        //得到设置bottom sheet 控件
        mLlBottomSheet = findViewById(R.id.bottom_sheet);

        // init the bottom sheet behavior
        mBottomSheetBehavior = BottomSheetBehavior.from(mLlBottomSheet);

        // change the state of the bottom sheet
//        mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
//        mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
//        mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
//
//        // set the peek height
//        mBottomSheetBehavior.setPeekHeight(340);
//
//        // set hideable or not
//        mBottomSheetBehavior.setHideable(false);

        // set callback for changes
        mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {

            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {

            }
        });
        //使用setFitToContents(false)才可以使用,展开一半的状态
        mBottomSheetBehavior.setFitToContents(false);//展开后开度填充Parent的高度
        mBottomSheetBehavior.setHalfExpandedRatio(0.45f);
        //setFitToContents 为false时，展开后距离顶部的位置（Parent会以PaddingTop填充）
//        mBottomSheetBehavior.setExpandedOffset(64);//设置完全展开的时候距离toolbar/actionbar的距离


        mBottomSheetBehavior.addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                switch (newState) {
                    case BottomSheetBehavior.STATE_DRAGGING:
                        Log.i(TAG, "onStateChanged: STATE_DRAGGING");
                        break;
                    case BottomSheetBehavior.STATE_SETTLING:
                        Log.i(TAG, "onStateChanged: STATE_SETTLING");
                        break;
                    case BottomSheetBehavior.STATE_EXPANDED:
                        Log.i(TAG, "onStateChanged: STATE_EXPANDED");
                        break;
                    case BottomSheetBehavior.STATE_COLLAPSED:
                        Log.i(TAG, "onStateChanged: STATE_COLLAPSED");
                        break;
                    case BottomSheetBehavior.STATE_HIDDEN:
                        Log.i(TAG, "onStateChanged: STATE_HIDDEN");
                        break;
                    case BottomSheetBehavior.STATE_HALF_EXPANDED:
                        Log.i(TAG, "onStateChanged: STATE_HALF_EXPANDED");
                        break;
                }
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {
                Log.i(TAG, "onSlide: " + slideOffset);
            }
        });
    }

    public void onclick(View view) {
        int id = view.getId();
        if (id == R.id.col) {
            mBottomSheetBehavior.setHideable(true);
            mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
        } else if (id == R.id.exp) {
            mBottomSheetBehavior.setHideable(false);
            mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        }
    }
}